package vn.mahjonggame.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import vn.mahjonggame.metadata.Player;
import vn.mahjonggame.metadata.ResponseCode;
import vn.mahjonggame.metadata.Tile;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class Common {
	
	public SFSObject makeResponse(ResponseCode code, SFSObject data)
	{
		SFSObject sfsObj = new SFSObject();
		sfsObj.putInt("returnCode", code.ordinal());
		if (data == null)
			data = new SFSObject();
		sfsObj.putSFSObject("data", data);		
		
		return sfsObj;
	}
	
	public SFSObject makeResponseMessage(String message)
	{
		SFSObject response = new SFSObject();
		response.putUtfString("message", message);
					
		return response;
	}
	
	public int rollDice(){
		Random rand = new Random();
		return rand.nextInt(6) + 1;		
	}
		
	public User getUserByPlayerId(int playerId, List<User> users){
		for(Iterator<User> iUsers = users.iterator(); iUsers.hasNext();){
			User iUser = iUsers.next();
			if(iUser.getId() == playerId){
				return iUser;
			}
		}
		return null;
	}

	public List<Tile> getDistinctTileByType(int tileType, List<Tile> tiles){
		
		Set<Tile> tileSet = new HashSet<Tile>();
		List<Tile> tileList = new ArrayList<Tile>();
		TileComparator comparator = new TileComparator();
		
		for (Iterator<Tile> iTiles = tiles.iterator(); iTiles.hasNext();){
			Tile iTile = iTiles.next();
			if(iTile.getType().ordinal() == tileType){
				tileSet.add(iTile);
			}
		}
		
		tileList.addAll(tileSet);
		Collections.sort(tileList, comparator);
		
		return tileList;
	}
	
	public List<Tile> getTileByType(int tileType, List<Tile> tiles){
				
		List<Tile> tileList = new ArrayList<Tile>();
		TileComparator comparator = new TileComparator();
		
		for (Iterator<Tile> iTiles = tiles.iterator(); iTiles.hasNext();){
			Tile iTile = iTiles.next();
			if(iTile.getType().ordinal() == tileType){
				tileList.add(iTile);
			}
		}
				
		Collections.sort(tileList, comparator);		
		return tileList;
	}
	
	public List<Integer> getTileIndexsByType(int tileType, List<Tile> tiles){						
		List<Integer> tileIndexs = new ArrayList<Integer>();
		
		for (Iterator<Tile> iTiles = tiles.iterator(); iTiles.hasNext();){
			Tile iTile = iTiles.next();
			if(iTile.getType().ordinal() == tileType){
				tileIndexs.add(iTile.getIndex());
			}
		}				
		Collections.sort(tileIndexs);
		
		return tileIndexs;
	}
	
	public List<Tile> getTilesByIndex(int tileIndex, List<Tile> tiles){
		List<Tile> subTiles = new ArrayList<Tile>();
		
		for(Iterator<Tile> iTiles = tiles.iterator(); iTiles.hasNext();){
			Tile iTile = iTiles.next();
			if(iTile.getIndex() == tileIndex){
				subTiles.add(iTile);
			}
		}
		
		return subTiles;
	}
	
	public void removeTileByType(int tileType, List<Tile> tiles){
		List<Tile> tilesTmp = new ArrayList<Tile>();
		tilesTmp.addAll(tiles);
		
		for (Iterator<Tile> iTiles = tilesTmp.iterator(); iTiles.hasNext();){
			Tile iTile = iTiles.next();
			if(iTile.getType().ordinal() == tileType){
				tiles.remove(iTile);
			}
		}
	}
		
	public List<Integer> tileArrayToTileIdArray(List<Tile> tiles){
		List<Integer> tileIds = new ArrayList<Integer>();
		
		for(Iterator<Tile> iTiles = tiles.iterator(); iTiles.hasNext();){
			Tile iTile = iTiles.next();
			tileIds.add(iTile.getId());
		}
		
		Collections.sort(tileIds);
		
		return tileIds;
	}
	
	public String tileIdsIntToString(List<Tile> tiles){
		StringBuffer str = new StringBuffer();
		
		for(Iterator<Tile> iTiles = tiles.iterator(); iTiles.hasNext();){
			str.append(iTiles.next().getId());
		}
		
		return str.toString();
	}
	
	public List<List<Integer>> tilesArrayToTileIdsArray(List<List<Tile>> tiles){
		List<List<Integer>> tileIdss = new ArrayList<List<Integer>>();
		
		for(Iterator<List<Tile>> iTiless = tiles.iterator(); iTiless.hasNext();){
			List<Tile> iTiles = iTiless.next();		
			tileIdss.add(tileArrayToTileIdArray(iTiles));
		}
		
		return tileIdss;
	}
		
	public ISFSArray javaArrayToSFSArray(List<Tile> tiles){
		ISFSArray sFSArray = new SFSArray();
		
		for(Iterator<Tile> iTiles = tiles.iterator(); iTiles.hasNext();){
			Tile iTile = iTiles.next();
			ISFSObject iSFSObject = iTile.toSFSObject();			
			sFSArray.addSFSObject(iSFSObject);
		}
		
		return sFSArray;
	}
	
	public ISFSArray javaArraysToSFSArray(List<List<Tile>> tiles){
		ISFSArray sFSArray = new SFSArray();
		
		for(Iterator<List<Tile>> iListTiles = tiles.iterator(); iListTiles.hasNext();){
			List<Tile> iListTile = iListTiles.next();			
			ISFSArray sSubFSArray = javaArrayToSFSArray(iListTile);
			sFSArray.addSFSArray(sSubFSArray);
		}
		
		return sFSArray;
	}
	
	public static synchronized boolean isApiSuccess(SFSObject response)
	{
		if (response == null)
			return false;
		if (!response.containsKey("returnCode"))
			return false;		
		if (200 != Integer.parseInt(response.get("returnCode").getObject().toString()))
			return false;
		if (!response.containsKey("response"))
			return false;
		return true;
		
	}
	
	public String log(Room room, Player player, String handlerName, String content){
		StringBuffer str = new StringBuffer();
		str.append("Room: ")
			.append(room.getName())
			.append("-")
			.append(room.getId())			
			.append(" Player: ")
			.append(player.toString())
			.append(" ")
			.append(handlerName)
			.append(": ")
			.append(content);
		
		return str.toString();
	}
	
}
